import { StorageManager } from '@vben-core/shared/cache';

import { KeyEnum } from './apiKeys';
// dify知识库api
import difyInstance from './instance';

const AppOptions = import.meta.env;
const apiUrl = AppOptions.VITE_DIFY_IP;
const storageManager = new StorageManager({
  prefix: '',
  storageType: 'sessionStorage',
});
// 创建数据集接口
const createDataset = () => {
  difyInstance.defaults.headers.common.Authorization = `Bearer ${KeyEnum.DATASET}`;
  return difyInstance.post(`${apiUrl}/datasets`, {
    name: storageManager.getItem('userId'),
    permission: 'only_me',
  });
};

const retrieve = (datasetId: string, query: string) => {
  difyInstance.defaults.headers.common.Authorization = `Bearer ${KeyEnum.DATASET}`;
  const config = {
    search_method: 'hybrid_search',
    reranking_enable: false,
    weights: 0.7,
    top_k: 3,
    score_threshold_enabled: false,
    score_threshold: 0,
  };
  return difyInstance.post(`${apiUrl}/datasets/${datasetId}/retrieve`, {
    query,
    retrieval_model: config,
  });
};

export default {
  createDataset,
  retrieve,
};
